//
//  UIImage+SPExtension.m
//  Sport
//
//  Created by Cass on 2017/11/22.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "UIImage+SPExtension.h"

@implementation UIImage (SPExtension)
- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha{
    /**
     参数size为新创建的位图上下文的大小
     opaque 透明开关，如果图形完全不用透明，设置为YES以优化位图的存储
     scale 可以用[UIScreen mainScreen].scale来获取，但实际上设为0后，系统就会自动设置正确的比例了
    UIGraphicsBeginImageContextWithOptions(<#CGSize size#>, <#BOOL opaque#>, <#CGFloat scale#>)
     */
    //开启上下文
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
    //获取当前的上文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -rect.size.height);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetAlpha(ctx, alpha);
    CGContextDrawImage(ctx, rect, self.CGImage);
    //获取新的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)createImageWithColocr:(UIColor *)color fram:(CGRect)rect{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, [color CGColor]);
    CGContextFillRect(ctx, rect);
    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)getGradientImageWithSize:(CGSize)size locations:(const CGFloat [])locations components:(const CGFloat [])components count:(NSInteger)count{
    const CGFloat location[] = {0, 1};
    const CGFloat component[] = {
        0.32, 0.55, 1.0, 1.0,
        0.17, 0.86, 0.99, 1.0
    };
    locations = location;
    components = component;
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 创建色彩空间
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    /*指定渐变色
     space:颜色空间
     components:颜色数组,注意由于指定了RGB颜色空间，那么四个数组元素表示一个颜色（red、green、blue、alpha）
     如果有三个颜色则这个数组有4*3个元素
     locations:颜色所在位置（范围0~1），这个数组的个数不小于components中存放颜色的个数
     count:渐变个数，等于locations的个数
     */
    CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, count);
    // 释放颜色空间
    CGColorSpaceRelease(colorSpace);
    
    // 这里调节渐变方向，此时的渐变是上到下
    CGPoint startPoint = (CGPoint){0, size.height * 0.5};
    CGPoint endPoint = (CGPoint){size.width, size.height * 0.5};
    /*绘制线性渐变
     context:图形上下文
     gradient:渐变色
     startPoint:起始位置
     endPoint:终止位置
     options:绘制方式,kCGGradientDrawsBeforeStartLocation 开始位置之前就进行绘制，到结束位置之后不再绘制
     kCGGradientDrawsAfterEndLocation开始位置之前不进行绘制，到结束点之后继续填充
     */
    CGContextDrawLinearGradient(context, colorGradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
    
    CGGradientRelease(colorGradient);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage *)originalImage{
    UIGraphicsBeginImageContext(self.size);
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (instancetype)scaleImageWithSize:(CGSize)size{
    CGFloat maxWidth = self.size.width;
    CGFloat maxHeight = self.size.height;
    CGFloat w = size.width;
    CGFloat h = size.height;
    CGFloat imageWidth = 0;
    CGFloat imageHeight = 0;
    CGFloat rat = h/w;
    //如果截取区域的高相对较大
    if (h/maxHeight < w/maxWidth) {
        imageWidth = maxWidth;
        imageHeight = imageWidth * rat;
    }else{
        imageHeight = maxHeight;
        imageWidth = imageHeight / rat;
    }
    //从图片中间截取
    CGFloat x = fabs((maxWidth - imageWidth)/2);
    CGFloat y = fabs((maxHeight - imageHeight)/2);
    CGImageRef newImage = CGImageCreateWithImageInRect([self CGImage], CGRectMake(x, y, imageWidth, imageHeight));
    UIImage *resultImage = [UIImage imageWithCGImage:newImage];
    CGImageRelease(newImage);
    return resultImage;
}
@end
